1. Technical Field
The present invention relates generally to an improved data processing system, and in particular, to a method and apparatus for managing threads in a data processing system. Still more particularly, the present invention provides a method and apparatus for increasing performance in the handling of sleeping and waking threads.
2. Description of Related Art
A thread is a basic unit of central processing unit (CPU) utilization. A thread usually has a program counter, a register set, and a stack space. A thread shares with other threads its code section, data section, and operating system resources, such as open files and signals. These components are also known as a “task”. Some systems implement user threads in user level libraries, rather than via system calls, such that thread switching does not need to call the operating system and cause an interrupt to the kernel.
Threads operate, in many respects, in the same manner as processes. A thread may be in one of several states: ready, blocked, sleeping, running, or terminated. User threads in a user space are run by kernel threads in a kernel. A kernel thread is also referred to as a “virtual processor”. In some cases, a one-to-one model is used in which each user thread has a corresponding kernel thread. In other cases, an M:N model is used in which many user threads are run on a few kernel threads for increased performance. With this model, situations occur, such as blocking on a mutex, in which a kernel thread is no longer needed to run a particular user thread. A mutex is a locking mechanism involving the use of a programming flag used to grab and release an object. When data is acquired that cannot be shared or processing is started that cannot be performed simultaneously elsewhere in the system, the mutex is set to “lock,” which blocks other attempts to use it. The mutex is set to “unlock” when the data is no longer needed or the routine is finished. If no other user threads are currently runnable, this kernel thread will detach itself from that particular user thread and enter a sleeping state.
Detachment of a kernel thread to enter a sleep state results in a number of actions occurring. One action taken is that the kernel thread switches from a user stack to its own smaller stack. Additionally, the kernel thread sets up signal masking to block most signals. When the kernel thread is needed again, this thread will switch to the stack of the user thread and set up several thread specific attributes, such as signal masks.
The present invention recognizes that this currently used detachment and subsequent reattachment mechanism involves a large performance overhead. Each detachment or reattachment requires a system call to copy data from user space to kernel space or to kernel space from user space. Additionally, several locks are used in both the kernel library and in the kernel, resulting in possibly increasing lock contention. This type of detachment also involves potential signal handling problems. Specifically, a small window is present before the kernel thread blocks signals where the kernel thread might receive a signal while executing on its small kernel stack. The present invention also recognizes that a signal handler, which runs fine on a larger user thread stack, may overflow the smaller kernel thread stack corrupting the memory and/or causing the application to core dump.
These two problems of performance overhead and stack overflow are separate problems, but have a similar root cause. This cause is the detaching of “idle” kernel threads. Therefore, it would be advantageous to have an improved method, apparatus, and computer instructions for handling idle kernel threads in a manner that reduces performance overhead and avoids stack overflows.